/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.apps.epocket.manager;


import com.apps.epocket.model.MailBean;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.wideplay.warp.persist.Transactional;
import java.util.Collections;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.StatelessSession;
import org.hibernate.Transaction;

/**
 *
 * @author Administrator
 */
public class MailManager {

    Provider<SessionFactory> sp;

    @Inject
    public MailManager(Provider<SessionFactory> sp) {
        this.sp = sp;
    }

    @Transactional
    public void send(MailBean mail) {
        sp.get().getCurrentSession().save(mail);
    }

    public List<MailBean> getAllMail() {
        StatelessSession ss = null;
        try {
            ss = sp.get().openStatelessSession();
            return ss.getNamedQuery("MailBean.findAll").list();
        } catch (HibernateException ex) {
        } finally {
            if (ss != null)
                ss.close();
        }
        return Collections.emptyList();
    }

    public void delete(MailBean mail) {
        StatelessSession ss = null;
        try {
            ss = sp.get().openStatelessSession();
            Transaction tx = ss.beginTransaction();
            ss.delete(mail);
            tx.commit();
        } catch (HibernateException ex) {
        } finally {
            if (ss != null)
                ss.close();
        }
    }

}
